package com.feiyu.testdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button beancheck, write, read,write2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //write();
        beancheck = findViewById(R.id.hook测试);
        write = findViewById(R.id.write);
        read = findViewById(R.id.read);
        write2=findViewById(R.id.write2);

        beancheck.setOnClickListener(this);
        write.setOnClickListener(this);
        read.setOnClickListener(this);
        write2.setOnClickListener(this);
    }

    private void write() {
        Bean bean = new Bean();
        bean.isVip = false;
        bean.age = 20;
        bean.name = "小张";
        saveObject(this, "bean", bean);

        String toast = "写入成功：" + bean.age + "|" + bean.name + "|" + bean.isVip;
        Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
    }

    private void read() {

        Bean bean = (Bean) getObject(this, "bean");

        String toast = "读取成功：" + bean.age + "|" + bean.name + "|" + bean.isVip;
        Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
    }

    public static void saveObject(Context context, String str, Object obj) {
        if (context != null) {
            try {
                SharedPreferences.Editor edit = context.getSharedPreferences("config", 0).edit();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                new ObjectOutputStream(byteArrayOutputStream).writeObject(obj);
                edit.putString(str, bytesToHexString(byteArrayOutputStream.toByteArray()));
               // Log.i("HEX: ", bytesToHexString(byteArrayOutputStream.toByteArray()));
                edit.apply();
                edit.commit();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //未被hook的方法
    public static void saveObject2(Context context, String str, Object obj) {
        if (context != null) {
            try {
                SharedPreferences.Editor edit = context.getSharedPreferences("config", 0).edit();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                new ObjectOutputStream(byteArrayOutputStream).writeObject(obj);
                edit.putString(str, bytesToHexString(byteArrayOutputStream.toByteArray()));
                // Log.i("HEX: ", bytesToHexString(byteArrayOutputStream.toByteArray()));
                edit.apply();
                edit.commit();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private static String hookTest(){
        return "还没有被hook";
    }
    private static String bytesToHexString(byte[] bArr) {
        if (bArr == null) {
            return null;
        }
        if (bArr.length == 0) {
            return "";
        }
        StringBuffer stringBuffer = new StringBuffer(bArr.length);
        for (byte b : bArr) {
            String hexString = Integer.toHexString(b & 255);
            if (hexString.length() < 2) {
                stringBuffer.append(0);
            }
            stringBuffer.append(hexString.toUpperCase());
        }
        return stringBuffer.toString();
    }


    public static Object getObject(Context context, String str) {
        if (context != null) {
            try {
                SharedPreferences sharedPreferences = context.getSharedPreferences("config", 0);
                if (sharedPreferences.contains(str)) {
                    String string = sharedPreferences.getString(str, "");
                    if (TextUtils.isEmpty(string)) {
                        return null;
                    }
                    return new ObjectInputStream(new ByteArrayInputStream(stringToBytes(string))).readObject();
                }
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
            } catch (ClassNotFoundException e3) {
                e3.printStackTrace();
            }
        }
        return null;
    }

    private static byte[] stringToBytes(String trim) {
        trim = trim.toUpperCase().trim();
        if (trim.length() % 2 != 0) {
            return null;
        }
        final byte[] array = new byte[trim.length() / 2];
        int n;
        for (int i = 0; i < trim.length(); i = n + 1) {
            int char1 = trim.charAt(i);
            if (char1 >= 48 && char1 <= 57) {
                char1 -= 48;
            }
            else {
                if (char1 < 65 || char1 > 70) {
                    return null;
                }
                char1 -= 55;
            }
            n = i + 1;
            int char2 = trim.charAt(n);
            if (char2 >= 48 && char2 <= 57) {
                char2 -= 48;
            }
            else {
                if (char2 < 65 || char2 > 70) {
                    return null;
                }
                char2 -= 55;
            }
            array[n / 2] = (byte)(char1 * 16 + char2);
        }

        return array;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hook测试:
                Toast.makeText(this, hookTest(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.write:
                write();
                break;
            case R.id.read:
                read();
                break;
            case R.id.write2:
                Bean bean = new Bean();
                bean.isVip = false;
                bean.age = 20;
                bean.name = "小张";
                saveObject2(this, "bean", bean);
                String toast = "写入成功：" + bean.age + "|" + bean.name + "|" + bean.isVip;
                Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
                break;
        }
    }
}